package com.campus.backend.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.backend.bean.banner.BannerQueryParam;
import com.campus.backend.bean.banner.CreateBannerBean;
import com.campus.backend.bean.banner.UpdateBannerBean;
import com.campus.backend.entity.Banner;
import com.campus.backend.mapper.BannerMapper;
import com.campus.backend.vo.banner.BannerVo;
import com.campus.common.bean.common.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 轮播图表 服务类
 * </p>
 *
 * @author campus
 * @since 2024-01-01
 */
@Service
public class BannerService extends ServiceImpl<BannerMapper, Banner> {

    /**
     * 分页查询轮播图列表
     *
     * @param param 查询参数
     * @return 分页结果
     */
    public PageInfo<BannerVo> getBannerListPage(BannerQueryParam param) {
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        PageInfo<Banner> pageInfo = new PageInfo<>(getBannerList(param));

        // 转换为VO对象
        List<BannerVo> voList = pageInfo.getList().stream().map(banner -> {
            BannerVo vo = new BannerVo();
            BeanUtil.copyProperties(banner, vo);
            
            // 设置类型名称
            switch (banner.getType()) {
                case 1:
                    vo.setTypeName("首页轮播图");
                    break;
                case 2:
                    vo.setTypeName("商品轮播图");
                    break;
                case 3:
                    vo.setTypeName("活动轮播图");
                    break;
                default:
                    vo.setTypeName("未知类型");
                    break;
            }
            
            // 设置状态名称
            vo.setStatusName(banner.getStatus() == 1 ? "启用" : "禁用");
            
            return vo;
        }).collect(java.util.stream.Collectors.toList());

        return PageUtil.convertPageInfo(pageInfo, voList);
    }

    /**
     * 查询轮播图列表
     *
     * @param param 查询参数
     * @return 轮播图列表
     */
    private List<Banner> getBannerList(BannerQueryParam param) {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        
        if (param.getType() != null) {
            queryWrapper.eq(Banner::getType, param.getType());
        }
        
        if (param.getStatus() != null) {
            queryWrapper.eq(Banner::getStatus, param.getStatus());
        }
        
        if (StringUtils.hasText(param.getTitle())) {
            queryWrapper.like(Banner::getTitle, param.getTitle());
        }
        
        queryWrapper.orderByDesc(Banner::getSort, Banner::getCreateTime);
        
        return this.list(queryWrapper);
    }

    /**
     * 根据ID获取轮播图详情
     *
     * @param bannerId 轮播图ID
     * @return 轮播图VO
     */
    public BannerVo getBannerById(Integer bannerId) {
        Banner banner = this.getById(bannerId);
        if (banner == null) {
            throw new RuntimeException("轮播图不存在");
        }
        
        BannerVo vo = new BannerVo();
        BeanUtil.copyProperties(banner, vo);
        
        // 设置类型名称
        switch (banner.getType()) {
            case 1:
                vo.setTypeName("首页轮播图");
                break;
            case 2:
                vo.setTypeName("商品轮播图");
                break;
            case 3:
                vo.setTypeName("活动轮播图");
                break;
            default:
                vo.setTypeName("未知类型");
                break;
        }
        
        // 设置状态名称
        vo.setStatusName(banner.getStatus() == 1 ? "启用" : "禁用");
        
        return vo;
    }

    /**
     * 创建轮播图
     *
     * @param createBannerBean 轮播图信息
     * @return 轮播图ID
     */
    public Integer createBanner(CreateBannerBean createBannerBean) {
        Banner banner = new Banner();
        BeanUtil.copyProperties(createBannerBean, banner);
        
        // 设置默认值
        if (banner.getSort() == null) {
            banner.setSort(0);
        }
        if (banner.getStatus() == null) {
            banner.setStatus(1);
        }
        
        banner.setCreateTime(LocalDateTime.now());
        banner.setUpdateTime(LocalDateTime.now());
        
        this.save(banner);
        return banner.getId();
    }

    /**
     * 更新轮播图
     *
     * @param updateBannerBean 轮播图信息
     */
    public void updateBanner(UpdateBannerBean updateBannerBean) {
        Banner existBanner = this.getById(updateBannerBean.getId());
        if (existBanner == null) {
            throw new RuntimeException("轮播图不存在");
        }
        
        Banner banner = new Banner();
        BeanUtil.copyProperties(updateBannerBean, banner);
        banner.setUpdateTime(LocalDateTime.now());
        
        this.updateById(banner);
    }

    /**
     * 删除轮播图
     *
     * @param bannerId 轮播图ID
     */
    public void deleteBanner(Integer bannerId) {
        Banner banner = this.getById(bannerId);
        if (banner == null) {
            throw new RuntimeException("轮播图不存在");
        }
        
        this.removeById(bannerId);
    }

    /**
     * 批量删除轮播图
     *
     * @param bannerIds 轮播图ID列表
     */
    public void batchDeleteBanner(List<Integer> bannerIds) {
        if (bannerIds == null || bannerIds.isEmpty()) {
            throw new RuntimeException("请选择要删除的轮播图");
        }
        
        this.removeByIds(bannerIds);
    }

    /**
     * 更新轮播图状态
     *
     * @param bannerId 轮播图ID
     * @param status 状态
     */
    public void updateBannerStatus(Integer bannerId, Integer status) {
        Banner banner = this.getById(bannerId);
        if (banner == null) {
            throw new RuntimeException("轮播图不存在");
        }
        
        LambdaUpdateWrapper<Banner> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Banner::getId, bannerId)
                .set(Banner::getStatus, status)
                .set(Banner::getUpdateTime, LocalDateTime.now());
        
        this.update(updateWrapper);
    }
}
